package com.lagou.edu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by shixi  on 2021/1/28
 */
@RestController
@RequestMapping("/email")
public class EmailConctroller {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String emailFrom;

    @RequestMapping("/{email}/{code}")
    public boolean send(
            @PathVariable(value = "email") String email,
            @PathVariable(value = "code") String code){

        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(emailFrom);
            simpleMailMessage.setTo(email);
            simpleMailMessage.setSubject("注册验证码");
            simpleMailMessage.setText("验证码：" + code);
            javaMailSender.send(simpleMailMessage);

            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
